### Analyses for The Moral Roots of Partisan Division ###

### SSI Primary Analyses

# preliminary steps
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load Amelia for imputed data
library(Amelia) 
require(Amelia)

# load Zelig to run models with imputed data from Amelia 
library(Zelig) 
require(Zelig)

# extra packages
library(mice)
library(mitools)
library(xtable)
library(weights)
library(survey)

# set seed for replication
set.seed(723327)

# load SSI data
SSI <- read.dta("The Moral Roots of Partisan Division_SSI Data.dta")
SSI7 <- SSI[c(33:62)]

# impute SSI data
ssi7.a <- amelia(SSI7, m = 25)
ssi7.imp <- imputationList(ssi7.a$imputations)
# write.amelia(ssi7.a, separate = FALSE, file.stem = "The Moral Roots of Partisan Division_Imputed SSI Data", format = "dta")

# 2016 party gap 
results.p <- with(ssi7.imp, lm(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fp <- MIcombine(results.p)
mitools.rp <- summary(mitools.fp)
mitools.rp <- cbind(mitools.rp, df = mitools.fp$df)
mitools.rp

# check
z.party <- zelig(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                 + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                   ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.party)

# 2016 candidate gap
results.l <- with(ssi7.imp, lm(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fl <- MIcombine(results.l)
mitools.rl <- summary(mitools.fl)
mitools.rl <- cbind(mitools.rl, df = mitools.fl$df)
mitools.rl

# check
z.leader <- zelig(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.leader)

# 2016 relationship distance
results.d <- with(ssi7.imp, lm(ssi_ap_dateI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fd <- MIcombine(results.d)
mitools.rd <- summary(mitools.fd)
mitools.rd <- cbind(mitools.rd, df = mitools.fd$df)
mitools.rd

# check
z.date <- zelig(ssi_ap_dateI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                  ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.date)

# 2016 social media distance
results.b <- with(ssi7.imp, lm(ssi_ap_blockI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fb <- MIcombine(results.b)
mitools.rb <- summary(mitools.fb)
mitools.rb <- cbind(mitools.rb, df = mitools.fb$df)
mitools.rb

# check
z.block <- zelig(ssi_ap_blockI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                 + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                   ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.block)

# 2016 anger
results.a <- with(ssi7.imp, lm(ssi_ap_angryI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fa <- MIcombine(results.a)
mitools.ra <- summary(mitools.fa)
mitools.ra <- cbind(mitools.ra, df = mitools.fa$df)
mitools.ra

# check
z.anger <- zelig(ssi_ap_angryI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                 + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                   ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.anger)

# 2016 incivility
results.f <- with(ssi7.imp, lm(ssi_ap_funI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.ff <- MIcombine(results.f)
mitools.rf <- summary(mitools.ff)
mitools.rf <- cbind(mitools.rf, df = mitools.ff$df)
mitools.rf

# check
z.fun <- zelig(ssi_ap_funI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
               + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                 ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.fun)

# 2016 antagonism
results.u <- with(ssi7.imp, lm(ssi_ap_upsetI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                  ssi_female + ssi_age_cor + ssi_educ1))
mitools.fu <- MIcombine(results.u)
mitools.ru <- summary(mitools.fu)
mitools.ru <- cbind(mitools.ru, df = mitools.fu$df)
mitools.ru

# check
z.upset <- zelig(ssi_ap_upsetI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                 + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                   ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi7.a)
summary(z.upset)

### SSI Secondary Analyses

## Casewise Deletion, Including Independents 

# 2016 party gap
party_feel.ni <- lm(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                      ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                      ssi_age_cor + ssi_educ1, data = SSI)
summary(party_feel.ni)

# 2016 candidate gap 
leader_feel.ni <- lm(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                       ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                       ssi_age_cor + ssi_educ1, data = SSI)
summary(leader_feel.ni)

# 2016 relationship distance
date.ni <- lm(ssi_ap_dateI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                ssi_age_cor + ssi_educ1, data = SSI)
summary(date.ni)

# 2016 social media distance
block.ni <- lm(ssi_ap_blockI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                 ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                 ssi_age_cor + ssi_educ1, data = SSI)
summary(block.ni)

# 2016 anger
anger.ni <- lm(ssi_ap_angryI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                 ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                 ssi_age_cor + ssi_educ1, data = SSI)
summary(anger.ni)

# 2016 incivility
fun.ni <- lm(ssi_ap_funI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
               ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
               ssi_age_cor + ssi_educ1, data = SSI)
summary(fun.ni)

# 2016 antagonism
upset.ni <- lm(ssi_ap_upsetI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem +
                 ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + ssi_female + 
                 ssi_age_cor + ssi_educ1, data = SSI)
summary(upset.ni)

## With Interaction Term Between Partisan Strength and Propensity to Moralize

# add interaction term to imputed data
ssi7i.a <- transform(ssi7.a, propmoral_partextrem = ssi_avg_prop_mor * ssi_party_extrem)
ssi7i.imp <- imputationList(ssi7i.a$imputations)

# 2016 party gap
results.p.int <- with(ssi7i.imp, lm(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + 
                                      ssi_ideol_extrem +
                                      ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 + 
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fp.int <- MIcombine(results.p.int)
mitools.rp.int <- summary(mitools.fp.int)
mitools.rp.int <- cbind(mitools.rp.int, df = mitools.fp.int$df)
mitools.rp.int

# check
z.out.p.int <- zelig(ssi_party_feel_dif  ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.p.int)

# 2016 candidate gap
results.l.int <- with(ssi7i.imp, lm(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fl.int <- MIcombine(results.l.int)
mitools.rl.int <- summary(mitools.fl.int)
mitools.rl.int <- cbind(mitools.rl.int, df = mitools.fl.int$df)
mitools.rl.int

# check
z.out.l.int <- zelig(ssi_leader_feel_dif  ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.l.int)

# 2016 relationship distance
results.d.int <- with(ssi7i.imp, lm(ssi_ap_dateI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fd.int <- MIcombine(results.d.int)
mitools.rd.int <- summary(mitools.fd.int)
mitools.rd.int <- cbind(mitools.rd.int, df = mitools.fd.int$df)
mitools.rd.int

# check
z.out.d.int <- zelig(ssi_ap_dateI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.d.int)

# 2016 social media distance
results.b.int <- with(ssi7i.imp, lm(ssi_ap_blockI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fb.int <- MIcombine(results.b.int)
mitools.rb.int <- summary(mitools.fb.int)
mitools.rb.int <- cbind(mitools.rb.int, df = mitools.fb.int$df)
mitools.rb.int

# check
z.out.b.int <- zelig(ssi_ap_blockI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.b.int)

# 2016 anger
results.a.int <- with(ssi7i.imp, lm(ssi_ap_angryI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fa.int <- MIcombine(results.a.int)
mitools.ra.int <- summary(mitools.fa.int)
mitools.ra.int <- cbind(mitools.ra.int, df = mitools.fa.int$df)
mitools.ra.int

# check 
z.out.a.int <- zelig(ssi_ap_angryI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.a.int)

## 2016 incivility
results.f.int <- with(ssi7i.imp, lm(ssi_ap_funI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.ff.int <- MIcombine(results.f.int)
mitools.rf.int <- summary(mitools.ff.int)
mitools.rf.int <- cbind(mitools.rf.int, df = mitools.ff.int$df)
mitools.rf.int

# check 
z.out.f.int <- zelig(ssi_ap_funI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.f.int)

# 2016 antagonism
results.u.int <- with(ssi7i.imp, lm(ssi_ap_upsetI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                      ssi_female + ssi_age_cor + ssi_educ1 +
                                      ssi_avg_prop_mor*ssi_party_extrem))
mitools.fu.int <- MIcombine(results.u.int)
mitools.ru.int <- summary(mitools.fu.int)
mitools.ru.int <- cbind(mitools.ru.int, df = mitools.fu.int$df)
mitools.ru.int

# check 
z.out.u.int <- zelig(ssi_ap_upsetI ~ ssi_avg_prop_mor + ssi_party_extrem + ssi_ideol_extrem 
                     + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                       ssi_female + ssi_age_cor + ssi_educ1 +
                       propmoral_partextrem, model = "ls", data = ssi7i.a)
summary(z.out.u.int)

## Multiple Imputation, Excluding Independents

# preliminary steps to remove and reload data before creating imputed datasets without independents
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load Amelia for imputed data
library(Amelia) 
require(Amelia)

# load Zelig to run models with imputed data from Amelia 
library(Zelig) 
require(Zelig)

# extra packages
library(mice)
library(mitools)
library(xtable)
library(weights)
library(survey)

# set seed for replication
set.seed(723327)

# load SSI data again
SSI <- read.dta("The Moral Roots of Partisan Division_SSI Data.dta")

# define data without independents
SSI.noIs <- subset(SSI, SSI$partyid_7 != 4)

# create variable for standardized partisan strength 
SSI.noIs$ssi_party_extrem1 <- 
  (SSI.noIs$party_extrem - min(SSI.noIs$party_extrem, na.rm=T))/
  (max(SSI.noIs$party_extrem, na.rm = T) - min(SSI.noIs$party_extrem, na.rm = T))

SSI3.noIs <- SSI.noIs[c(33:76)]
SSI37.noIs <- SSI3.noIs[c(-3,-24,-25,-26,-27,-28,-29,-30,-39,-40,-41,-42,-43)]

# impute data
ssi3.a <- amelia(SSI37.noIs, m = 25)
ssi3.imp <- imputationList(ssi3.a$imputations)

# 2016 party gap
results.pfeelI <- with(ssi3.imp, lm(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem1 + 
                                          ssi_ideol_extrem + ssi_rel_attend + ssi_b_a + ssi_pol_know +
                                          ssi_white + ssi_female + ssi_age_cor + ssi_educ1))
mitools.pfI <- MIcombine(results.pfeelI)
mitoolsr.pfI <- summary(mitools.pfI)
mitoolsr.pfI <- cbind(mitoolsr.pfI, df = mitools.pfI$df)
mitoolsr.pfI

# check
z.party.I <- zelig(ssi_party_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.party.I)

# 2016 candidate gap
results.lfeelI <- with(ssi3.imp, lm(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem1 + 
                                          ssi_ideol_extrem + ssi_rel_attend + ssi_b_a + ssi_pol_know +
                                          ssi_white + ssi_female + ssi_age_cor + ssi_educ1))
mitools.lfI <- MIcombine(results.lfeelI)
mitoolsr.lfI <- summary(mitools.lfI)
mitoolsr.lfI <- cbind(mitoolsr.lfI, df = mitools.lfI$df)
mitoolsr.lfI

# check
z.leader.I <- zelig(ssi_leader_feel_dif ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                   + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                     ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.leader.I)

# 2016 relationship distance
results.dateI <- with(ssi3.imp, lm(ssi_ap_date ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                                       + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                         ssi_female + ssi_age_cor + ssi_educ1))
mitools.fitdI <- MIcombine(results.dateI)
mitools.resdI <- summary(mitools.fitdI)
mitools.resdI <- cbind(mitools.resdI, df = mitools.fitdI$df)
mitools.resdI

# check
z.date.I <- zelig(ssi_ap_date ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                    + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                      ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.date.I)

# 2016 social media distance
results.blockI <- with(ssi3.imp, lm(ssi_ap_block ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                                        + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                          ssi_female + ssi_age_cor + ssi_educ1))
mitools.fitbI <- MIcombine(results.blockI)
mitools.resbI <- summary(mitools.fitbI)
mitools.resbI <- cbind(mitools.resbI, df = mitools.fitbI$df)
mitools.resbI

# check
z.block.I <- zelig(ssi_ap_block ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.block.I)

# 2016 anger
results.angerI <- with(ssi3.imp, lm(ssi_ap_angry ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                                        + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                          ssi_female + ssi_age_cor + ssi_educ1))
mitools.fitaI <- MIcombine(results.angerI)
mitools.resaI <- summary(mitools.fitaI)
mitools.resaI <- cbind(mitools.resaI, df = mitools.fitaI$df)
mitools.resaI

# check
z.anger.I <- zelig(ssi_ap_angry ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.anger.I)

# 2016 incivility
results.funI <- with(ssi3.imp, lm(ssi_ap_fun ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                                      + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                        ssi_female + ssi_age_cor + ssi_educ1))
mitools.fitfI <- MIcombine(results.funI)
mitools.resfI <- summary(mitools.fitfI)
mitools.resfI <- cbind(mitools.resfI, df = mitools.fitfI$df)
mitools.resfI

# check
z.fun.I <- zelig(ssi_ap_fun ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.fun.I)

# 2016 antagonism
results.upsetI <- with(ssi3.imp, lm(ssi_ap_upset ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                                        + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                                          ssi_female + ssi_age_cor + ssi_educ1))
mitools.fituI <- MIcombine(results.upsetI)
mitools.resuI <- summary(mitools.fituI)
mitools.resuI <- cbind(mitools.resuI, df = mitools.fituI$df)
mitools.resuI

# check
z.upset.I <- zelig(ssi_ap_upset ~ ssi_avg_prop_mor + ssi_party_extrem1 + ssi_ideol_extrem 
                  + ssi_rel_attend + ssi_b_a + ssi_pol_know + ssi_white + 
                    ssi_female + ssi_age_cor + ssi_educ1, model="ls", data=ssi3.a)
summary(z.upset.I)

### Analyses for The Moral Roots of Partisan Division ###

### ANES Primary Analyses for Candidate Gap

# preliminary steps
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load Amelia for imputed data
library(Amelia) 
require(Amelia)

# load Zelig to run models with imputed data from Amelia 
library(Zelig) 
require(Zelig)

# extra packages
library(mice)
library(mitools)
library(xtable)
library(weights)
library(survey)

# set seed for replication
set.seed(723327)

# load ANES EGSS data
EGSS <- read.dta("The Moral Roots of Partisan Division_ANES EGSS Data.dta")
egssdata <- EGSS[c(-7,-8,-13, -27, -28, -29, -30, -31)]
egss.nomissingwght <- subset(egssdata, egssdata$anes_wght != "NA")

# impute ANES EGSS data
egss.imp.nm <- amelia(egss.nomissingwght, m = 25)
e.imp.nm <- imputationList(egss.imp.nm$imputations)
# write.amelia(egss.imp.nm, separate = FALSE, file.stem = "The Moral Roots of Partisan Division_Imputed ANES EGSS Data", format = "dta")

# define survey weight
setwt <- svydesign(id=~1, data=e.imp.nm, weights=~anes_wght)

# 2012 candidate gap
results.l2 <- with(setwt, svyglm(anes_rating_dif2 ~ anes_prop_moralize + anes_party_extrem + 
                                   anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                   anes_pol_know + anes_white + anes_female + anes_age +
                                   anes_education, family = gaussian()))
mitools.fitl2 <- MIcombine(results.l2)
mitools.resl2 <- summary(mitools.fitl2)
mitools.resl2 <- cbind(mitools.resl2, df = mitools.fitl2$df)
mitools.resl2

### ANES Secondary Analyses for 2012 Candidate Gap

## Casewise Deletion, Including Independents 

# define survey weight
setup1 <- svydesign(id = ~1, data = egss.nomissingwght, weights=~egss.nomissingwght$anes_wght)

# 2012 candidate gap
l2.gap <- svyglm(anes_rating_dif2 ~ anes_prop_moralize + anes_party_extrem + anes_ideol_extrem + 
                   anes_rel_attend1 + anes_evangel + anes_pol_know + anes_white + anes_female + 
                   anes_age + anes_education, design = setup1)
summary(l2.gap)

## With Interaction Term Between Partisan Strength and Propensity to Moralize

# add interaction term to imputed data
egss7i.a <- transform(egss.imp.nm, anes_propmoral_partextrem = anes_prop_moralize*anes_party_extrem)
egss7i.imp <- imputationList(egss7i.a$imputations)

# define survey weight
setwt.7i <- svydesign(id=~1, data=egss7i.imp, weights=~anes_wght)

# 2012 candidate gap
results.l2.int <- with(setwt.7i, svyglm(anes_rating_dif2 ~ anes_prop_moralize + anes_party_extrem + 
                                          anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                          anes_pol_know + anes_white + anes_female + anes_age +
                                          anes_education + 
                                          anes_propmoral_partextrem, family = gaussian()))
mitools.fitl2.int <- MIcombine(results.l2.int)
mitools.resl2.int <- summary(mitools.fitl2.int)
mitools.resl2.int <- cbind(mitools.resl2.int, df = mitools.fitl2.int$df)
mitools.resl2.int

# check 
results.l2.int1 <- with(setwt.7i, svyglm(anes_rating_dif2 ~ anes_prop_moralize + anes_party_extrem + 
                                           anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                           anes_pol_know + anes_white + anes_female + anes_age +
                                           anes_education + 
                                           anes_prop_moralize*anes_party_extrem, family = gaussian()))
mitools.fitl2.int1 <- MIcombine(results.l2.int1)
mitools.resl2.int1 <- summary(mitools.fitl2.int1)
mitools.resl2.int1 <- cbind(mitools.resl2.int1, df = mitools.fitl2.int1$df)
mitools.resl2.int1

## Multiple Imputation, Excluding Independents

# preliminary steps to remove and reload data before creating imputed datasets without independents
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load Amelia for imputed data
library(Amelia) 
require(Amelia)

# load Zelig to run models with imputed data from Amelia 
library(Zelig) 
require(Zelig)

# extra packages
library(mice)
library(mitools)
library(xtable)
library(weights)
library(survey)

# set seed for replication
set.seed(723327)

# load ANES EGSS data again
EGSS <- read.dta("The Moral Roots of Partisan Division_ANES EGSS Data.dta")
egssdata.e <- EGSS[c(-7,-8,-13,-15,-30,-31)]

# define data without independents
egssdata.ei <- subset(egssdata.e, egssdata.e$anes_party_id3 != 2)
egss.nomissingwght.noI <- subset(egssdata.ei, egssdata.ei$anes_wght != "NA")

# impute ANES EGSS data
egss.imp.noI <- amelia(egss.nomissingwght.noI, m = 25)
e.imp.noI <- imputationList(egss.imp.noI$imputations)

# define survey weight
set.noI <- svydesign(id=~1, data=e.imp.noI, weights=~anes_wght)

# 2012 candidate gap
results.2l.noI <- with(set.noI, svyglm(anes_rating_dif2 ~ anes_prop_moralize + anes_party_extrem1 + 
                                         anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                         anes_pol_know + anes_white + anes_female + anes_age +
                                         anes_education, family = gaussian()))
mitools.fit2l.noI <- MIcombine(results.2l.noI)
mitools.res2l.noI <- summary(mitools.fit2l.noI)
mitools.res2l.noI <- cbind(mitools.res2l.noI, df = mitools.fit2l.noI$df)
mitools.res2l.noI

### ANES Primary Analyses for 2012 Partisan Divide in Presidential Approval

# preliminary steps to remove and reload data before creating imputed datasets with dichotomous variable for party id
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load Amelia for imputed data
library(Amelia) 
require(Amelia)

# load Zelig to run models with imputed data from Amelia 
library(Zelig) 
require(Zelig)

# extra packages
library(mice)
library(mitools)
library(xtable)
library(weights)
library(survey)

# set seed for replication
set.seed(723327)

# load ANES EGSS data again
EGSS <- read.dta("The Moral Roots of Partisan Division_ANES EGSS Data.dta")

# define data without independents
EGSS.noIs <- subset(EGSS, EGSS$anes_party_id3 != 2)

# create variable for standardized partisan strength and job approval
EGSS.noIs$anes_party_extrem3 <- 
  (EGSS.noIs$party_extrem - min(EGSS.noIs$party_extrem, na.rm=T))/
  (max(EGSS.noIs$party_extrem, na.rm = T) - min(EGSS.noIs$party_extrem, na.rm = T))

EGSS.noIs$anes_job_approve <- (EGSS.noIs$job_approve - min(EGSS.noIs$job_approve, na.rm=T))/
  (max(EGSS.noIs$job_approve, na.rm = T) - min(EGSS.noIs$job_approve, na.rm = T))

EGSS.noIs.s <- EGSS.noIs[c(-5,-8,-13,-15,-18,-28,-29,-30,-31)]
egss.nomissingwght.noIsp <- subset(EGSS.noIs.s, EGSS.noIs.s$anes_wght != "NA")

# impute ANES data and add interaction term to imputed data
egss.imp.noIsp <- amelia(egss.nomissingwght.noIsp, m = 25)
egss.imp.noIsp <- transform(egss.imp.noIsp, rep_moral = anes_repub_d_no * anes_prop_moralize)
e.imp.noIsp <- imputationList(egss.imp.noIsp$imputations)
# write.amelia(egss.imp.noIsp, separate = FALSE, file.stem = "The Moral Roots of Partisan Division_Imputed ANES EGSS Data_for PA", format = "dta")

# set survey weight
d.noIs <- svydesign(id=~1, data=e.imp.noIsp, weights=~anes_wght) 

# 2012 partisan divide in presidential approval
resultspa <- with(d.noIs, svyglm(anes_job_approve ~ anes_repub_d_no + 
                                   anes_prop_moralize + anes_party_extrem3 + 
                                   anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                   anes_pol_know + anes_white + anes_female + anes_age +
                                   anes_education + rep_moral, family = gaussian()))
mitools.fitpa <- MIcombine(resultspa)
mitools.respa <- summary(mitools.fitpa)
mitools.respa <- cbind(mitools.respa, df = mitools.fitpa$df)
mitools.respa

# check
resultspa1 <- with(d.noIs, svyglm(anes_job_approve ~ anes_repub_d_no + 
                                   anes_prop_moralize + anes_party_extrem3 + 
                                   anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                   anes_pol_know + anes_white + anes_female + anes_age +
                                   anes_education + 
                                   anes_repub_d_no*anes_prop_moralize, family = gaussian()))
mitools.fitpa1 <- MIcombine(resultspa1)
mitools.respa1 <- summary(mitools.fitpa1)
mitools.respa1 <- cbind(mitools.respa1, df = mitools.fitpa1$df)
mitools.respa1

### ANES Secondary Analyses for 2012 Partisan Divide in Presidential Approval

## Casewise Deletion, Including Independents 

# define survey weight
setup13 <- svydesign(id = ~1, data = egss.nomissingwght.noIsp, weights=~egss.nomissingwght.noIsp$anes_wght)

# 2012 partisan divide in presidential approval
pa.gap <- svyglm(anes_job_approve ~ anes_repub_d_no + anes_prop_moralize + anes_party_extrem3 + 
                   anes_ideol_extrem + anes_rel_attend1 + anes_evangel +  anes_pol_know + anes_white + 
                   anes_female + anes_age +
                   anes_education + anes_repub_d_no*anes_prop_moralize, setup13)
summary(pa.gap)

## With Interaction Term Between Partisan Strength and Propensity to Moralize

# add interaction term to imputed data
egss.imp.noIsp.i <- transform(egss.imp.noIsp, anes_propmoral_partextrem = anes_prop_moralize*anes_party_extrem3)
egss.imp.noIsp.i.imp <- imputationList(egss.imp.noIsp.i$imputations)

# define survey weight
setwt.73i <- svydesign(id=~1, data=egss.imp.noIsp.i.imp, weights=~anes_wght)

# 2012 partisan divide in presidential approval
resultspa.int <- with(setwt.73i, svyglm(anes_job_approve ~ anes_repub_d_no + 
                                          anes_prop_moralize + anes_party_extrem3 + 
                                          anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                          anes_pol_know + anes_white + anes_female + anes_age +
                                          anes_education + rep_moral +
                                          anes_propmoral_partextrem, family = gaussian()))
mitools.fitpa.int <- MIcombine(resultspa.int)
mitools.respa.int <- summary(mitools.fitpa.int)
mitools.respa.int <- cbind(mitools.respa.int, df = mitools.fitpa.int$df)
mitools.respa.int

# check 
resultspa.int1 <- with(setwt.73i, svyglm(anes_job_approve ~ anes_repub_d_no + 
                                           anes_prop_moralize + anes_party_extrem3 + 
                                           anes_ideol_extrem + anes_rel_attend1 + anes_evangel + 
                                           anes_pol_know + anes_white + anes_female + anes_age +
                                           anes_education + rep_moral +
                                           anes_prop_moralize*anes_party_extrem3, family = gaussian()))
mitools.fitpa.int1 <- MIcombine(resultspa.int1)
mitools.respa.int1 <- summary(mitools.fitpa.int1)
mitools.respa.int1 <- cbind(mitools.respa.int1, df = mitools.fitpa.int1$df)
mitools.respa.int1


### SSI Overview of Affective Polarization in Everyday Life Measures

# preliminary steps
rm(list=ls(all=TRUE))
setwd("~/Desktop/Submission Material/Data") 
library(foreign)

# load SSI data
APscale <- read.dta("The Moral Roots of Partisan Division_SSI Data.dta")

# calculate mean and sd of all affective polarization scores
all.tog <- c(APscale$ssi_ap_dateI, APscale$ssi_ap_blockI, APscale$ssi_ap_funI,
         APscale$ssi_ap_upsetI, APscale$ssi_ap_angryI)
mean(all.tog, na.rm = T)
sd(all.tog, na.rm = T)

# subset for Democrats and Republicans
SSI_D <- subset(APscale, APscale$ssi_partyid_3 == 1)
SSI_R <- subset(APscale, APscale$ssi_partyid_3 == 3)

# to better assess overall hostility and distance, t-tests, means and sds calculated using data where Don't Know responses are excluded

# 2016 relationship distance 
t.test(SSI_D$ssi_ap_dateI, SSI_R$ssi_ap_dateI)
mean(SSI_D$ssi_ap_dateI, na.rm = T)
sd(SSI_D$ssi_ap_dateI, na.rm = T)
mean(SSI_R$ssi_ap_dateI, na.rm = T)
sd(SSI_R$ssi_ap_dateI, na.rm = T)

hist(SSI_D$ssi_ap_dateI)
hist(SSI_R$ssi_ap_dateI)

# 2016 social media distance
t.test(SSI_D$ssi_ap_blockI, SSI_R$ssi_ap_blockI)
mean(SSI_D$ssi_ap_blockI, na.rm = T)
sd(SSI_D$ssi_ap_blockI, na.rm = T)
mean(SSI_R$ssi_ap_blockI, na.rm = T)
sd(SSI_R$ssi_ap_blockI, na.rm = T)

hist(SSI_D$ssi_ap_blockI)
hist(SSI_R$ssi_ap_blockI)

# 2016 anger
t.test(SSI_D$ssi_ap_angryI, SSI_R$ssi_ap_angryI)
mean(SSI_D$ssi_ap_angryI, na.rm = T)
sd(SSI_D$ssi_ap_angryI, na.rm = T)
mean(SSI_R$ssi_ap_angryI, na.rm = T)
sd(SSI_R$ssi_ap_angryI, na.rm = T)

hist(SSI_D$ssi_ap_angryI)
hist(SSI_R$ssi_ap_angryI)

# 2016 incivility
t.test(SSI_D$ssi_ap_funI, SSI_R$ssi_ap_funI)
mean(SSI_D$ssi_ap_funI, na.rm = T)
sd(SSI_D$ssi_ap_funI, na.rm = T)
mean(SSI_R$ssi_ap_funI, na.rm = T)
sd(SSI_R$ssi_ap_funI, na.rm = T)

hist(SSI_D$ssi_ap_funI)
hist(SSI_R$ssi_ap_funI)

# 2016 antagonism
t.test(SSI_D$ssi_ap_upsetI, SSI_R$ssi_ap_upsetI)
mean(SSI_D$ssi_ap_upsetI, na.rm = T)
sd(SSI_D$ssi_ap_upsetI, na.rm = T)
mean(SSI_R$ssi_ap_upsetI, na.rm = T)
sd(SSI_R$ssi_ap_upsetI, na.rm = T)

hist(SSI_D$ssi_ap_upsetI)
hist(SSI_R$ssi_ap_upsetI)

#  calculate mean and sd of average affective polarization scores
x <- cbind(SSI_D$ssi_ap_dateI, SSI_D$ssi_ap_blockI, SSI_D$ssi_ap_funI,
            SSI_D$ssi_ap_upsetI, SSI_D$ssi_ap_angryI)

SSI_D$ssi_avg_affect_pol1 <- rowMeans(x, na.rm = F)

x1 <- cbind(SSI_R$ssi_ap_dateI, SSI_R$ssi_ap_blockI, SSI_R$ssi_ap_funI,
           SSI_R$ssi_ap_upsetI, SSI_R$ssi_ap_angryI)

SSI_R$ssi_avg_affect_pol1 <- rowMeans(x1, na.rm = F)

t.test(SSI_D$ssi_avg_affect_pol1, SSI_R$ssi_avg_affect_pol1)
mean(SSI_D$ssi_avg_affect_pol1, na.rm = T)
sd(SSI_D$ssi_avg_affect_pol1, na.rm = T)
mean(SSI_R$ssi_avg_affect_pol1, na.rm = T)
sd(SSI_R$ssi_avg_affect_pol1, na.rm = T)

# calculate mean and sd of all affective polarization scores, for Democrats
D.tog <- c(SSI_D$ssi_ap_dateI, SSI_D$ssi_ap_blockI, SSI_D$ssi_ap_funI,
             SSI_D$ssi_ap_upsetI, SSI_D$ssi_ap_angryI)
mean(D.tog, na.rm = T)
sd(D.tog, na.rm = T)

# calculate mean and sd of all affective polarization scores, for Republicans
R.tog <- c(SSI_R$ssi_ap_dateI, SSI_R$ssi_ap_blockI, SSI_R$ssi_ap_funI,
           SSI_R$ssi_ap_upsetI, SSI_R$ssi_ap_angryI)
mean(R.tog, na.rm = T)
sd(R.tog, na.rm = T)

## Cronbach's alpha

# load packages
library(psy)
library(psych)

# to better assess scale reliability, Cronbach's alpha calculated using data where Don't Know responses are coded 0, rather than excluded

# Cronbach's alpha for five items
z <- cbind(APscale$ap_datei11, APscale$ap_blocki11, APscale$ap_funi11,
           APscale$ap_upseti11, APscale$ap_angryi11)

cronbach(z)
alpha(z)

# Cronbach's alpha for five items, among Democratic identifiers 
zd <- cbind(SSI_D$ap_datei11, SSI_D$ap_blocki11, SSI_D$ap_funi11,
            SSI_D$ap_upseti11, SSI_D$ap_angryi11)

cronbach(zd)
alpha(zd)

# Cronbach's alpha for five items, among Republican identifiers 
zr <- cbind(SSI_R$ap_datei11, SSI_R$ap_blocki11, SSI_R$ap_funi11,
            SSI_R$ap_upseti11, SSI_R$ap_angryi11)

cronbach(zr)
alpha(zr)

## correlations and regressions comparing propensity to moralize and partisan extremity

# for SSI data

cor.test(APscale$ssi_avg_prop_mor, APscale$ssi_party_extrem)

reg <- lm(ssi_avg_prop_mor ~ ssi_party_extrem, data = APscale)
summary(reg)

# for ANES EGSS data

# need to load ANES EGSS data
EGSS <- read.dta("The Moral Roots of Partisan Division_ANES EGSS Data.dta")
egssdata <- EGSS[c(-7,-8,-13, -27, -28, -29, -30, -31)]
egss.nomissingwght <- subset(egssdata, egssdata$anes_wght != "NA")

cor.test(egss.nomissingwght$anes_prop_moralize, egss.nomissingwght$anes_party_extrem)
wtd.cor(egss.nomissingwght$anes_prop_moralize, egss.nomissingwght$anes_party_extrem,
        weight=egss.nomissingwght$anes_wght)

rega <- lm(anes_prop_moralize ~ anes_party_extrem, 
           data = egss.nomissingwght, weights = anes_wght)
summary(rega)
